package com.sl.au.service.impl;


import com.sl.au.entity.CustomerVisitPerson;
import com.sl.au.entity.CustomerVisiting;
import com.sl.au.entity.CustomerVisitingCost;

import com.sl.au.repository.CustomerVisitPersonRepository;
import com.sl.au.repository.CustomerVisitingCostRepository;
import com.sl.au.repository.CustomerVisitingRepository;

import com.sl.au.service.CustomerVisitingService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service("CustomerVisitingService")
@Transactional
public class CustomerVisitingServiceImpl extends BaseServiceImpl implements CustomerVisitingService {

	@Autowired
	CustomerVisitingRepository customerVisitingRepository;
	
	@Autowired
	CustomerVisitingCostRepository customerVisitingCostRepository;
	
	@Autowired
	CustomerVisitPersonRepository customerVisitPersonRepository;

	@Override
	public CustomerVisitingRepository getCustomerVisitingRepository() {
		// TODO Auto-generated method stub
		return this.customerVisitingRepository;
	}
	
	@Override
	public CustomerVisitPersonRepository getCustomerVisitPersonRepository() {
		// TODO Auto-generated method stub
		return this.customerVisitPersonRepository;
	}

	@Override
	public CustomerVisitingCostRepository getCustomerVisitingCostRepository() {
		// TODO Auto-generated method stub
		return this.customerVisitingCostRepository;
	}

	
	
	@Override
	public List<CustomerVisitPerson> getVisitPerson(int start, int length, String key, String visitId) {
		Page<CustomerVisitPerson> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if (StringUtils.hasText(key)) {
			pages = customerVisitPersonRepository.findVisitPerson(visitId, key, pageable);
		} else {
			pages = customerVisitPersonRepository.findVisitPerson(visitId, pageable);
		}
		return pages.getContent();
	}

	@Override
	public List<CustomerVisitingCost> getVisitingCost(int start, int length, String key, String visitId) {
		Page<CustomerVisitingCost> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		if (StringUtils.hasText(key)) {
			pages = customerVisitingCostRepository.findVisitCost(visitId, key, pageable);
		} else {
			pages = customerVisitingCostRepository.findVisitCost(visitId, pageable);
		}
		return pages.getContent();
	}
	@Override
	public List<CustomerVisiting> getVisit(int start, int length, String key, String dataFilter, String superior, String areaName, String userId) {
		Page<CustomerVisiting> pages = null;
		Pageable pageable=null;
		if(dataFilter.equals("所有")){
			pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "saleArea.superiorArea");
			pages = customerVisitingRepository.findVisit(key, pageable);
		}else if(dataFilter.equals("本部门")){
			pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
			pages = customerVisitingRepository.findVisit(key, superior, pageable);
		}else if(dataFilter.equals("本片区")){
			pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
			pages = customerVisitingRepository.findVisit(key, superior, areaName, pageable);
		}else if(dataFilter.equals("本人")){
			pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
			pages = customerVisitingRepository.findVisit1(key, userId, pageable);
		}
		return pages.getContent();
	}
	
	@Override
	public long getVisitCount(String key, String dataFilter, String superior, String areaName, String userId) {
		long count=0;
		if(dataFilter.equals("所有")){
			count= customerVisitingRepository.getCount(key);
		}else if(dataFilter.equals("本部门")){
			count = customerVisitingRepository.getCount(key, superior);
		}else if(dataFilter.equals("本片区")){
			count = customerVisitingRepository.getCount(key, superior, areaName);
		}else if(dataFilter.equals("本人")){
			count = customerVisitingRepository.getCount1(key, userId);
		}
		return count;
	}

	@Override
	public List<CustomerVisiting> getVisitById(int start, int length, String key, String customerId) {
		Page<CustomerVisiting> pages = null;
		Pageable pageable = new PageRequest(start / length, length, Sort.Direction.DESC, "createTime");
		pages=customerVisitingRepository.findCustomerVisit(customerId, key, pageable);	
		return pages.getContent();
	}

	

	

}
